﻿// ---------------------------------------------------------------------
// Lớp SPlayer
//
// Nội dung:
//    + Định nghĩa người chơi
//
// Copyright (c) 2009 YugiOH
//
// Nguyễn Hữu Lê Trọng Tín      YM: ilu2009             ĐT: 0906520611 
// Võ Phước Hậu                 YM: phuochauspkt        ĐT: 0979202707
// ---------------------------------------------------------------------


using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Yugi.Client.Card;
using Common;
using ServerYuGi.Converter;
using ServerYuGi.Block;
using ServerYuGi.PythonRegister;

namespace ServerYuGi
{
    public class SPlayer
    {
        public int ID { get; set; }
        public static List<SCard> BoBaiDuocChon { get; set; }
        public SSupervisor MySupervisor { get; set; }
        public YUser MyConnection { get; set; }
        public SPlayer()
        {

            BoBaiDuocChon = YLoadCardFromForder.LoadAllFileFromForder(YConverter.Cardpath);            
            SoBaiChuaChia = BoBaiDuocChon;
            SoBaiTrenTay = new List<SCard>();
            SoBaiTrenBan = new List<SCard>();
            SoBaiDaChet = new List<SCard>();
            Block = new BlockData();
        }
        //--------------------------------------------------------------------//
        public int SoDiem { get; set; }
        public int TrangThai { get; set; }
        public List<SCard> SoBaiTrenTay { get; set; }
        public List<SCard> SoBaiDaChet { get; set; }
        public List<SCard> SoBaiChuaChia { get; set; }
        public List<SCard> SoBaiTrenBan { get; set; }
        //----------------------------------ILU-------------------------------//
        public BlockData Block { get; set; }
        //----------------------------------ILU-------------------------------//

        Random r = new Random();

        public int SoSummonTrenBan
        {
            get
            {
                int dem = 0;
                foreach (SCard item in SoBaiTrenBan)
                {
                    if (item.MyYBaseCard.CardType == YType.Monster)
                        dem++;                   
                }
                return dem;
            }
        }
        public int SoTrapHoacSpellTrenBan
        {
            get
            {
                int dem = 0;
                foreach (SCard item in SoBaiTrenBan)
                {
                    if (item.MyYBaseCard.CardType == YType.Spell || item.MyYBaseCard.CardType == YType.Trap)
                        dem++;
                }
                return dem;
            }
        }

        /// <summary>
        /// Lấy 1 lá bài trên bàn
        /// </summary>
        /// <param name="ID">ID lá cần lấy</param>
        /// <returns>lá bài lấy được</returns>
        public SCard LayCardTrenBan(int id)
        {
           
            foreach (SCard item in this.SoBaiTrenBan)
            {
                if (item.MyYBaseCard.ID == id)
                    return item;
            }
            return null;
        }

        /// <summary>
        /// Lấy 1 lá bài trên tay
        /// </summary>
        /// <param name="ID">ID lá cần lấy</param>
        /// <returns>lá bài lấy được</returns>
        public SCard LayCardTrenTay(int id)
        {

            foreach (SCard item in this.SoBaiTrenTay)
            {
                if (item.MyYBaseCard.ID == id)
                    return item;
            }
            return null;
        }       

        /// <summary>
        /// Lay La Bai Trong Bo bai
        /// </summary>
        /// <param name="id">Id lá bài cần lấy</param>
        /// <returns>Lá bài nếu có</returns>
        public SCard LayCardTrongBoBai(int id)
        {
            foreach (SCard item in this.SoBaiChuaChia)
            {
                if (item.MyYBaseCard.ID == id)
                    return item;
            }
            return null;
        }

        public SCard LayCardTrongMo(int id)
        {

            foreach (SCard item in this.SoBaiDaChet)
            {
                if (item.MyYBaseCard.ID == id)
                    return item;
            }
            return null;
        }       
        public SCard LayCardInAll(int id)
        {
            SCard card = LayCardTrenBan(id);
            if (card != null)
                return card;
            card = LayCardTrenTay(id);
            if (card != null)
                return card;
            card = LayCardTrongMo(id);
            if (card != null)
                return card;
            card = LayCardTrongBoBai(id);
            if (card != null)
                return card;
            return null;
        }

        /// <summary>
        /// Xóa lá bài bị chết
        /// </summary>
        /// <param name="CardXoa">lá bài cần xóa</param>
        public void XoaLaBaiHySinh(SCard CardXoa)
        {
            Block.DuocPhep_TanCong.Remove(CardXoa.MyYBaseCard.ID);  
            SoBaiDaChet.Add(CardXoa);
            SoBaiTrenBan.Remove(CardXoa);
        }
        public void CheckXoaScript(SSupervisor super,SCard Card)
        {
            try
            {
                CardScript XMLCardScript = super.CScriptDB.GetScript(Card.MyYBaseCard.Card_TypeID);
                if (XMLCardScript == null)
                    return;
                if (XMLCardScript.IsAutoDie == YBool.True)
                {
                    super.EffectTurnManager.RemoveScript(Card.MyYBaseCard.ID);
                    super.pyregister.RemoveIDinStore(Card);
                }
            }catch(Exception e)
            {
                return;
            }
        }      
        public void XoaBaiTrenTay()
        {

        }
        public bool XoaBaiTrongMo(int id)
        {
            SCard tmp = LayCardTrongMo(id);
            if (tmp != null)
            {
                SoBaiDaChet.Remove(tmp);
                return true;
            }
            return false;
        }
        /// <summary>
        /// Rút 1 lá bài
        /// </summary>
        /// <param name="LaBaiRut">lá bài cần rút</param>
        public SCard RutLaBai()
        {
            //if (SoBaiChuaChia.Count == 0)
            //    return null;
            //int vitri = r.Next(0, SoBaiChuaChia.Count);
            //SCard LaBaiRut = SoBaiChuaChia[vitri];
            //SoBaiTrenTay.Add(LaBaiRut);
            //SoBaiChuaChia.Remove(LaBaiRut);
            //return LaBaiRut;

            //@Ilu: Fake for Simulation
            return Fake_RutLaBai();
        }

        public SCard Fake_RutLaBai()
        {
            if (SoBaiChuaChia.Count == 0)
                return null;
            ReplayViewer.LogReader.LogItem item = ReplayViewer.Program.log_reader.NextLogItem();

            //ToDo: Inc Turn ID
            if (item.Tag == ReplayViewer.LogTag.TURN_ID)
            {
               
                Console.WriteLine();
                Console.WriteLine("***************************************************");
                Console.WriteLine("*              TURN   {0}                         *",item.Data[0]);
                Console.WriteLine("***************************************************");
                item = ReplayViewer.Program.log_reader.NextLogItem();
                
            }

            if (item.Tag != ReplayViewer.LogTag.RUT_BAI && item.Tag != ReplayViewer.LogTag.API_RUTBAI)
                throw new Exception("Log file Format Error");
            int id;
            if (item.Tag == ReplayViewer.LogTag.RUT_BAI)
                id = Int32.Parse(item.Data[0]);
            else
                id = Int32.Parse(item.Data[1]);
            SCard LaBaiRut = this.LayCardTrongBoBai(id);
            SoBaiTrenTay.Add(LaBaiRut);
            SoBaiChuaChia.Remove(LaBaiRut);
            Console.WriteLine("-- Rut Bai  {0}", item.Data[0]);
            return LaBaiRut;
        }

        /// <summary>
        /// Lên 1 lá bài
        /// </summary>
        /// <param name="Card">lá bài được lên</param>
        public void LenBai(SCard Card)
        {
            SoBaiTrenBan.Add(Card);
            SoBaiTrenTay.Remove(Card);
        }

        //---------------------------ILU-------------------------------
        /// <summary>
        /// Init Danh Sach Bai Duoc Doi Trang Thai(Goi Moi Khi Change Turn)
        /// </summary>
        public void InitDuocPhepDoiTrangThai()
        {
            foreach (SCard item in this.SoBaiTrenBan)
            {
                Block.DuocPhep_DoiTrangThai.Add(item.MyYBaseCard.ID);
            }
        }

        /// <summary>
        /// Init Danh Sach Bai Duoc Tan Cong(Goi Moi Khi Change Turn)
        /// </summary>
        public void InitDuocPhepTanCong()
        {
            foreach (SCard item in this.SoBaiTrenBan)
            {
                Block.DuocPhep_TanCong.Add(item.MyYBaseCard.ID);
            }
        }

        public bool CheckIDCardTrenBan(int Id)
        {
            foreach (SCard item in SoBaiTrenBan)
            {
                if (item.MyYBaseCard.ID == Id)
                    return true;
            }
            return false;
        }
        public bool CheckIDCardTrenTay(int Id)
        {
            foreach (SCard item in SoBaiTrenTay)
            {
                if (item.MyYBaseCard.ID == Id)
                    return true;
            }
            return false;
        }
        public bool CheckIDCardTrongMo(int Id)
        {
            foreach (SCard item in SoBaiDaChet)
            {
                if (item.MyYBaseCard.ID == Id)
                    return true;
            }
            return false;
        }
            public bool CheckIDCardTrongBoBai(int Id)
        {
            foreach (SCard item in SoBaiChuaChia)
            {
                if (item.MyYBaseCard.ID == Id)
                    return true;
            }
            return false;
        }
        public bool CheckIDInAll(int id)
        {
            if (CheckIDCardTrenBan(id))
                return true;
            if (CheckIDCardTrenTay(id))
                return true;
            if (CheckIDCardTrongMo(id))
                return true;
            if (CheckIDCardTrongBoBai(id))
                return true;
            return false;
        }


        public void MoveFromDeck2Hand(int id)
        {
            SCard card = LayCardTrongBoBai(id);
            if(card==null)          
                return;
            SoBaiTrenTay.Add(card);
            SoBaiChuaChia.Remove(card);
        }

        public void MoveFromDeck2Table(int id)
        {
            SCard card = LayCardTrongBoBai(id);
            if (card == null)          
                return;
            
            SoBaiTrenBan.Add(card);
            SoBaiChuaChia.Remove(card);
        }
        public void MoveFromDeck2Graveyard(int id)
        {
            SCard card = LayCardTrongBoBai(id);
            if (card == null)           
                return;
            
            SoBaiDaChet.Add(card);
            SoBaiChuaChia.Remove(card);
        }
        public void MoveFromDeck2OutZone(int id)
        {
            SCard card = LayCardTrongBoBai(id);
            if (card == null)
                return;

            SoBaiChuaChia.Remove(card);
        }

        public void MoveFromHand2Table(int id)
        {
            SCard card = LayCardTrenTay(id);
            if (card == null)         
                return;

            SoBaiTrenBan.Add(card);
            SoBaiTrenTay.Remove(card);
        }

        public void MoveFromHand2Deck(int id)
        {
            SCard card = LayCardTrenTay(id);
            if (card == null)            
                return;

            SoBaiChuaChia.Add(card);
            SoBaiTrenTay.Remove(card);
        }
        public void MoveFromHand2Graveyard(int id)
        {
            SCard card = LayCardTrenTay(id);
            if (card == null)
                return;

            SoBaiDaChet.Add(card);
            SoBaiTrenTay.Remove(card);
        }
        public void MoveFromHand2OutZone(int id)
        {
            SCard card = LayCardTrenTay(id);
            if (card == null)
                return;

            SoBaiTrenTay.Remove(card);
        }
        public void MoveFromTable2Deck(int id)
        {
            SCard card = LayCardTrenBan(id);
            if (card == null)
                return;
            SoBaiChuaChia.Add(card);
            SoBaiTrenBan.Remove(card);
        }
        public void MoveFromTable2Hand(int id)
        {
            SCard card = LayCardTrenBan(id);
            if (card == null)
                return;
            SoBaiTrenTay.Add(card);
            SoBaiTrenBan.Remove(card);
        }
        public void MoveFromTable2Graveyard(int id)
        {
            SCard card = LayCardTrenBan(id);
            if (card == null)
                return;
            SoBaiDaChet.Add(card);
            SoBaiTrenBan.Remove(card);
        }
        public void MoveFromTable2OutZone(int id)
        {
            SCard card = LayCardTrenBan(id);
            if (card == null)
                return;
            
            SoBaiTrenBan.Remove(card);
        }

        public void MoveFromGraveyard2Deck(int id)
        {
            SCard card = LayCardTrongMo(id);
            if (card == null)
                return;
            SoBaiChuaChia.Add(card);
            SoBaiDaChet.Remove(card);
        }

        public void MoveFromGraveyard2Hand(int id)
        {
            SCard card = LayCardTrongMo(id);
            if (card == null)
                return;
            SoBaiTrenTay.Add(card);
            SoBaiDaChet.Remove(card);
        }

        public void MoveFromGraveyard2Table(int id)
        {
            SCard card = LayCardTrongMo(id);
            if (card == null)
                return;
            SoBaiTrenBan.Add(card);
            SoBaiDaChet.Remove(card);
        }

        public void MoveFromGraveyard2OutZone(int id)
        {
            SCard card = LayCardTrongMo(id);
            if (card == null)
                return;
            SoBaiDaChet.Remove(card);
        }

       public int GetIndexCard_InHand(int IdCard)
       {
           for (int i = 0; i < SoBaiTrenTay.Count;i++ )
           {
               if (SoBaiTrenTay[i].MyYBaseCard.ID == IdCard)
                   return i;
           }
           return -1;
       }


        public int GetRandomID_InHand()
        {
            int tmp = r.Next(0, SoBaiTrenTay.Count);
            return SoBaiTrenTay[tmp].MyYBaseCard.ID;
        }
        public int GetRandomID_InDeck()
        {
            int tmp = r.Next(0, SoBaiChuaChia.Count);
            return SoBaiChuaChia[tmp].MyYBaseCard.ID;
        }
        public int GetRandomID_InTable()
        {
            int tmp = r.Next(0, SoBaiTrenBan.Count);
            return SoBaiTrenBan[tmp].MyYBaseCard.ID;
        }
        public int GetRandomID_InGraveyard()
        {
            int tmp = r.Next(0, SoBaiDaChet.Count);
            return SoBaiDaChet[tmp].MyYBaseCard.ID;
        }

        public void AddCardToBoBaiChuaChia(int cardtype_id)
        {
            YBaseCard normal = YConverter.GetCard(cardtype_id);
            SCard card = new SCard(normal);
            SoBaiChuaChia.Add(card);
            Console.WriteLine("Insert card : cardtype_id = " + cardtype_id);
        }

        public void F5()
        {
            BoBaiDuocChon = YLoadCardFromForder.LoadAllFileFromForder(YConverter.Cardpath); 
            SoBaiChuaChia = new List<SCard>(BoBaiDuocChon);
            SoBaiTrenTay.Clear();
            SoBaiTrenBan.Clear();
            SoBaiDaChet.Clear();
            Block = new BlockData();
            SoDiem = MySupervisor.Game.SoDiemBatDau;
        }
    }
}
